/**
 * FileIOHelper.java [V 1.0.0]
 * classes : com.xiaocq.iwordkiller.file.FileIOHelper
 * xiaocq Create at 2012-4-10 下午2:44:57
 */
package com.xiaocq.iwordkiller.file;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;

import android.content.Context;

/**
 * com.xiaocq.iwordkiller.file.FileIOHelper
 * @author Mystery
 * Create at 2012-4-10 下午2:44:57
 */
public class FileIOHelper {
	private Context context;
	
	public FileIOHelper(Context context){
		this.context = context;
	}
	
	/**
	 * 返回从assets中打开的文件的缓冲读入流
	 * @param fileName
	 * @return
	 * @throws IOException
	 */
	public BufferedReader getBufferedReaderFromAssets(String fileName) throws IOException{
		InputStream inputStream = context.getAssets().open(fileName);
		BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
		return reader;
	}
	
	/**
	 * 向指定文件中写入数据 
	 * @param fileName
	 * @param records
	 * @return
	 */
	public boolean writeIntoFile(String fileName, int[] records){
		String encoding = "UTF-8";
		int length = records.length;
		try{
			OutputStream outputStream = prepareOutputStreamToFile(fileName);
			BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(outputStream, encoding));
			for(int i=0; i < length; i++){
				if(records[i] != 0){
					writer.write(Integer.toString(records[i]));
					writer.newLine();
				}
			}
			writer.flush();
			writer.close();
		}catch(Exception e){
			e.printStackTrace();
			return false;
		}
		return true;
	}
	
	/**
	 * 从文件中取出测试数据
	 * @param fileName
	 * @param arraySize
	 * @return
	 */
	public int[] getRecordsFromFile(String fileName, int arraySize){
		int[] records = new int[arraySize];
		String encoding = "UTF-8";
		int index = 0;
		try{
			InputStream inputStream = prepareInputStreamFromFile(fileName);
			BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, encoding));
			while(reader.ready()){
				records[index++] = Integer.valueOf(reader.readLine());
			}
			reader.close();
		}catch(Exception e){
			e.printStackTrace();
		}
		
		return records;
	}
	
	/**
	 * 获取指定的来自文件的InputStream流
	 * @param fileName
	 * @return
	 * @throws FileNotFoundException
	 */
	public InputStream prepareInputStreamFromFile(String fileName) throws FileNotFoundException{
		InputStream inputStream;
		inputStream = context.openFileInput(fileName);
		return inputStream;
	}
	
	/**
	 * 获取指定的指向文件的OutputStream流
	 * @param fileName
	 * @return
	 * @throws FileNotFoundException
	 */
	public OutputStream prepareOutputStreamToFile(String fileName) throws FileNotFoundException{
		OutputStream outputStream;
		outputStream = context.openFileOutput(fileName, Context.MODE_PRIVATE);
		return outputStream;
	}
}
